package com.smartalarmclock.UI;

import java.util.ArrayList;
import com.smartalarmclock.UI.R;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

public class LogListAdapter extends ArrayAdapter<LogListItem> {

	private LayoutInflater m_Inflater;
	ArrayList<LogListItem> m_LogItems;
	Context m_Ctx;
	
	public LogListAdapter (Context i_Ctx, ArrayList<LogListItem> i_LogItems) {
		super(i_Ctx, R.layout.log_list, i_LogItems);
		m_LogItems = i_LogItems;
		m_Inflater = LayoutInflater.from(i_Ctx);
		m_Ctx = i_Ctx;
	}
	
	public int getCount() {
		return m_LogItems.size();
	}

	public LogListItem getItem(int position) {
		return m_LogItems.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			convertView = m_Inflater.inflate(R.layout.log_list_item, null);
			holder = new ViewHolder();
			holder.txt_itemName = (TextView)convertView.findViewById(R.id.profileName);
			holder.txt_lastUse = (TextView)convertView.findViewById(R.id.lastUse);
			holder.checkBox = (CheckBox)convertView.findViewById(R.id.logScreenCheckBox);
			convertView.setTag(holder);
			holder.checkBox.setOnClickListener(new View.OnClickListener() {
				 public void onClick(View v) { 
					 CheckBox cb = (CheckBox) v ;   
					 LogListItem log = (LogListItem) cb.getTag();  
					 log.setSelected(cb.isChecked());
					 }  
				 });   
			}  

		else {
			holder = (ViewHolder) convertView.getTag();
		}
		 
		LogListItem log = m_LogItems.get(position);
		holder.checkBox.setTag(log);
		holder.checkBox.setSelected(log.isSelected());
		holder.txt_itemName.setText(m_LogItems.get(position).getName());
		holder.txt_lastUse.setText(m_LogItems.get(position).getLastUse());
	
		Log.v("public View getView(int position, View convertView, ViewGroup parent)", "Finish");
		return convertView;
	}
	
	static class ViewHolder {
		TextView txt_itemName;
		TextView txt_lastUse;
		CheckBox checkBox;
	}
}
	